# -*- encoding=utf8 -*-
"""
RedFlag-TC000058   删除
删除单段视频场景
删除多段视频场景
"""
__author__ = "ldm"

import sys
import os

# 获取当前脚本所在目录.py
current_dir = os.path.dirname(os.path.abspath(__file__))
# 获取上级目录.air
parent_dir = os.path.dirname(current_dir)
# 获取上级目录air
grandparent_dir = os.path.dirname(parent_dir)
# 获取和air目录平级的conf、testcase目录路径
conf_dir = os.path.join(grandparent_dir, 'conf')
testcase_dir = os.path.join(grandparent_dir, 'testcase')
# 将conf、testcase目录添加到sys.path
sys.path.append(conf_dir)
sys.path.append(testcase_dir)

from PageFunction import CameraVideoShooter
from elements import *
auto_setup(__file__)

shooter = CameraVideoShooter()

log("--------------------------------删除单段视频场景--------------------------------------")
"""
进入相机拍摄
点击拍摄按钮
点击拍摄按钮（暂停）
点击删除按钮
点击取消
点击删除按钮
点击确认
断言
"""

shooter.enter_photograph()
shooter.click_element(shooting_button)
sleep(2.0)
shooter.click_element(shooting_button)
shooter.click_element(shooting_page_delete_video)
shooter.click_element(shooting_page_cancel_deleting_video)
shooter.click_element(shooting_page_delete_video)
shooter.click_element(shooting_page_confirm_to_delete_video)
shooter.assert_element_not_exists(shooting_page_shooting_duration, "断言失败，发现删除按钮，并未成功删除视频")


log("--------------------------------删除多段视频场景--------------------------------------")


def delete_until_disappear(delete_btn, confirm_btn=None, max_attempts=10, raise_on_error=True):
    attempts = 0

    while attempts < max_attempts:

        if not poco("cn.migu.video.clip.walle:id/clip_rc_progressbar_time").exists():
            print(f"删除成功，共尝试 {attempts} 次")
            return True

        try:
            shooter.click_element(delete_btn)

            if confirm_btn is not None:
                shooter.click_element(confirm_btn)

            attempts += 1

        except Exception as e:
            print(f"删除操作异常 (尝试 {attempts + 1}/{max_attempts}): {e}")
            if raise_on_error:
                raise
            attempts += 1

    print(f"删除失败，达到最大尝试次数 ({max_attempts})")
    return False
"""
拍摄多段视频
调用方法删除多段视频
断言
"""
shooter.multiple_dense_clicks(shooting_button, 10, 1)
delete_until_disappear(shooting_page_delete_video, shooting_page_confirm_to_delete_video, 11)
shooter.assert_element_not_exists(poco("cn.migu.video.clip.walle:id/clip_rc_progressbar_time"), "断言失败，发现视频时长，并未成功删除视频")

# 关闭应用
stop_app(UGC_package_name)